<template>
	<sc-dialog
		v-model="visible"
		show-fullscreen
		destroy-on-close
		:title="titleMap[mode]"
		width="750px"
		@close="close"
	>
		<el-form
			ref="formRef"
			label-width="100px"
			:model="formData"
			:rules="rules"
		>
			<el-form-item label="退货单号" prop="returnId">
	<el-select 
		v-model="formData.returnId" 
	placeholder="请选择退货单号" clearable > 
	<el-option 
	v-for="item in returnIdOptions" 
	:key="item.value" 
	:label="item.label" 
	:value="item.value" /> 
</el-select> 
</el-form-item> 
<el-form-item label="产品ID" prop="productId">
	<el-select 
		v-model="formData.productId" 
	placeholder="请选择产品ID" clearable > 
	<el-option 
	v-for="item in productIdOptions" 
	:key="item.value" 
	:label="item.label" 
	:value="item.value" /> 
</el-select> 
</el-form-item> 
<el-form-item label="退货数量" prop="returnQty"> 
	<el-input 
		v-model="formData.returnQty" 
		placeholder="请输入退货数量" 
		:maxlength="11" 
		show-word-limit 
		clearable 
	></el-input> 
</el-form-item> 
<el-form-item label="原订单单价" prop="originalPrice"> 
	<el-input 
		v-model="formData.originalPrice" 
		placeholder="请输入原订单单价" 
		:maxlength="15" 
		show-word-limit 
		clearable 
	></el-input> 
</el-form-item> 

		</el-form>

		<template #footer>
			<el-button @click="close">取 消</el-button>
			<el-button :loading="isSaveing" type="primary" @click="save">
				确 定
			</el-button>
		</template>
	</sc-dialog>
</template>
<script>
export default {
    emits: ['complete'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: "新增",
				edit: "编辑",
			},
			isSaveing: false,
			visible: false,
			formData: {
				id: 0,
				returnId:undefined, 
productId:undefined, 
returnQty:undefined, 
originalPrice:undefined, 

			},
			rules: {returnId: [{required: true,message: "请输入退货单号",trigger: "change",},],productId: [{required: true,message: "请输入产品ID",trigger: "change",},],returnQty: [{required: true,message: "请输入退货数量",trigger: "blur",},],originalPrice: [{required: true,message: "请输入原订单单价",trigger: "blur",},],},
			returnIdOptions:[],productIdOptions:[],
		};
	},
	mounted() {
	},
	methods: {
		async open(row) {
			if (!row) {
				this.mode = "add";
			} else {
				this.mode = "edit";
				var res = await this.$API.erpsalesreturnitem.model.get(row.id);
				this.formData = res.data;
			}
			this.visible = true;
		},
		save() {
			this.$refs.formRef.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					let res = null;
					if (this.formData.id === 0) {
						res = await this.$API.erpsalesreturnitem.add.post(this.formData);
					} else {
						res = await this.$API.erpsalesreturnitem.update.put(this.formData);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit("complete");
						this.visible = false;
						this.$message.success("保存成功");
					} else {
						this.$alert(res.message, "提示", { type: "error" });
					}
				}
			});
		},
		close() {
			this.formData = {
				id: 0,
				returnId:undefined, 
productId:undefined, 
returnQty:undefined, 
originalPrice:undefined, 

			};
			this.$refs.formRef.resetFields();
			this.visible = false;
		},
	},
};
</script>
